<template>
  <div>
    <div >
      <span style="color:red;">* </span><span>库存设置</span>
      <br>
      <br>
      <div   style="width: 100%">
        <a-table :columns="giveawaTableColumns"
                 size="small"
                 :data-source="giveawaGoodsList"
                 :row-key="(record) => record.goodsSpecId"
                 :pagination="false"
                 >

          <div slot="deltaStock" slot-scope="text, record">
<!--            <a-input-number :min="0" v-model="record.stock" :disabled="editable">-->
            <a-input-number  v-model="record.deltaStock" >


            </a-input-number>

          </div>
        </a-table>
        <br>
        <div style="float: right;">
          <a-button type="primary" v-show="giveawaGoodsList.length>0" @click="submitData">确定</a-button>
          <a-button style="margin-left: 15px;" @click="cancel">取消</a-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: "editGoods",
    components:{

    },
    watch:{
      //监听this.selectedRows下的库存字段
  /*    'selectedRows.stock': {
        handler(news, olds) {
          console.log(news,olds)
        },
        // immediate: true,
        deep: true
      },*/
    },
    created() {
      console.log(this.$route.query)
      this.id = this.$route.query.id
      this.goodsId = this.$route.query.goodsId
      this.goodsName = this.$route.query.goodsName
      this.goodsPicture = !!this.$route.query.goodsPicture ?this.$route.query.goodsPicture : this.vipIcon
      this.goodsType = this.$route.query.goodsType
      // this.goodsSpecId = this.$route.query.goodsSpecId
      // this.norm = this.$route.query.norm
      // this.selectedRowKeys.push(this.goodsSpecId)
      this.$get('goldenhome/gh/lottery_/admin/giving/goodsmanage/id',{id:this.id,goodsType: this.goodsType}).then((res)=>{
        if (res && res.returnCode === 1000) {
          console.log('***********',res)

          this.giveawaGoodsList = res.dataInfo.specList
          this.giveawaGoodsList.forEach((item,index)=>{
            this.$set(item,'goodsPicture',this.goodsPicture)
            this.$set(item,'goodsName',this.goodsName)
            this.$set(item,'goodsId',this.goodsId)
            this.$set(item,'deltaStock',0)
              // this.$set(item,'goodsSpecId',"0")
          })
              if (this.goodsType==3){
                  //   this.goodsPrice = goodsInfo[0]?.salePrice ?? (0+"")
                 this.giveawaGoodsList[0].goodsSpecId ="0"
                 this.giveawaGoodsList[0].norm ="统一规格"
              }

              if (this.goodsType==2){
                //   this.goodsPrice = goodsInfo[0]?.salePrice ?? (0+"")
                this.giveawaGoodsList[0].goodsStock ="不限量"
            }
        }
      })
    },
    computed:{

    },
    data(){
      return{
        id:'',
        goodsId :'',
        goodsName:'',
        goodsPicture:'',
        goodsType :1,
        vipIcon:require('@/assets/vipbgc/icon/middle_modalbox_icon_viplevel05@2x.png'),
        giveawaGoodsList:[],
        giveawaTableColumns: [
          {
            title: '商品名称',
            dataIndex: 'goodsName',
            key: 'goodsName',
            align: 'center',
            customRender:(text,row)=>{
              return <div class="product_td"><table-img src={row.goodsPicture}/><div class="product_name">{text}</div></div>
            }
          },
          {
            title: '商品规格',
            dataIndex: 'norm',
            key: 'norm',
            align: 'center',
            customRender:(text,row)=>{
              if (text==null && row.goodsSpecId ==1){
                  return "有效期:1年"
              }
              else if (text==null && row.goodsSpecId ==2){
                  return "有效期:3个月"
              }
              else  if (text==null && row.goodsSpecId ==3){
                  return "有效期:1个月"
              }
              else {
                  return  text
              }
          }
          },
          {
            title: '商品实际库存数',
            dataIndex: 'goodsStock',
            key: 'goodsStock'
          },
          {
            title: '增减赠品库存数量',
            dataIndex: 'deltaStock',
            key: 'deltaStock',
            align: 'center',
            scopedSlots: {customRender: 'deltaStock'}
          },
          {
            title: '赠品总库存数',
            dataIndex: 'stock',
            key: 'stock',
            align: 'center',
          },
          {
            title: '赠品剩余库存数',
            dataIndex: 'remainingStock',
            key: 'remainingStock',
            align: 'center',
          },
          {
            title: '赠品已被领取',
            dataIndex: 'prizePoolStock',
            key: 'prizePoolStock'
          },
          {
            title: '赠品已被使用',
            dataIndex: 'used',
            key: 'used',
          }
        ],
      }
    },
    methods:{
      submitData(){
        console.log("提交数据:",this.giveawaGoodsList)
        let allZero = this.giveawaGoodsList.filter(item => !item.deltaStock || item.deltaStock==0 || item.deltaStock==0)
        if(allZero.length == this.giveawaGoodsList.length) {
          console.log('全部为0')
        }
        let goodsId = this.giveawaGoodsList[0].goodsId
        /* let goodsSpecId = this.selectedRows[0].goodsSpecId  deltaStock
         let stock = this.selectedRows[0].stock*/
        this.$post('goldenhome/gh/lottery_/admin/giving/goodsmanage/update',{
           goodsId,
           goodsType:this.goodsType,
           goodsSpecList:this.giveawaGoodsList
        }).then((res)=>{
          if (res && res.returnCode === 1000) {
            console.log('***********')
            this.$message.success('修改成功')
            // 页面跳转一下
            this.$router.push({
              name: 'MarketingGiveaway',
            })
          }
        })
      },

      cancel(){
        this.$router.push({
          name: 'MarketingGiveaway',
        })
        console.log("取消,返回赠品主页面")
      }
    }
  }
</script>

<style scoped lang="less">
  .product_td{
    display: flex;
    align-items: center;
      .product_name{
        margin-left:6px;
      }
  }
</style>